import cryptoJs from "crypto-js";
import { isProd } from "../api/config";
// let keyOne = isProd ? "GKJY1803SHInt5fa" : "MIGfMA0GCSqGSIb3";
let keyOne = "BASIC886BJInt5fa"; //生产
let encrypt = isProd ? "PTyIvUL4LQHoPW+V0YiZHA==" : "PTyIvUL4LQHoPW+V0YiZHA==";
// let encrypt = isProd ? "PTyIvUL4LQHoPW+V0YiZHA==" : "yH0BFPE2Yjz5cVkCj1Zznw==";

export default {
  // 加密函數(废弃)
  // encryption (word) {
  //   let key = cryptoJs.enc.Hex.parse(keyOne)
  //   let enc = ''
  //   if (typeof word === 'string') {
  //     enc = cryptoJs.AES.encrypt(word, key, {
  //       // iv: iv
  //       mode: cryptoJs.mode.ECB,
  //       padding: cryptoJs.pad.Pkcs7
  //     })
  //   } else if (typeof word === 'object') {
  //     let data = JSON.stringify(word)
  //     enc = cryptoJs.AES.encrypt(data, key, {
  //       // iv: iv
  //       mode: cryptoJs.mode.ECB,
  //       padding: cryptoJs.pad.Pkcs7
  //     })
  //   }
  //   let encResult = enc.ciphertext.toString()
  //   return encResult
  // },

  encrypto(word) {
    if (word instanceof Object) {
      word = JSON.stringify(word);
    }
    let encrypted = cryptoJs.AES.encrypt(
      cryptoJs.enc.Utf8.parse(word),
      cryptoJs.enc.Utf8.parse(keyOne),
      {
        mode: cryptoJs.mode.ECB,
        padding: cryptoJs.pad.Pkcs7,
      }
    );
    return encrypted.toString();
  },
  // 解密函數
  decrypt(word) {
    let key = cryptoJs.enc.Hex.parse(keyOne);
    let dec = cryptoJs.AES.decrypt(cryptoJs.format.Hex.parse(word), key, {
      // vi: vi
      mode: cryptoJs.mode.ECB,
      padding: cryptoJs.pad.Pkcs7,
    });
    let decData = cryptoJs.enc.Utf8.stringify(dec);
    return decData;
  },
  encrypt,
};
